<!--------------------------------------------------------------
>//	Company:	HTHK
>//	NAME:	    Project.jsp
>//	TITLE:	    The maintence of project
>//	FUNCTION:	The save and update and delete page of project
>//	BY:	        Kodak Xu
>//	DATE:	    09/12/2003
>//	update_date:	16-01-2004
>//	update_by:	Poly Li
>//	update_date:	2004-2-13 17:36
>//	update_by:	Beyes Xia
>// --------------------------------------------------------------
>// MODIFICATION HISTORY
>// --------------------------------------------------------------
>//	CR Log #:  99999  BY:  XXXX   DATE:  dd/mm/yyyy
>//	ERROR:	XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
>//	FIX:	XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
>// --------------------------------------------------------------
-->

<%@ include file="../tssheader.jsp"   %>
<%@ page import="tss.project.*" %>
<%@ page import="com.ffpc.html.*" %>
<%@ page import="java.util.Date" %>
<%@ page import="tss.options.*"%>

<%    
	Calendar now = Calendar.getInstance();
	String year_now = String.valueOf(now.get(now.YEAR));
	String month_now = (now.get(now.MONTH)+1)>10?String.valueOf((now.get(now.MONTH)+1)):"0"+String.valueOf((now.get(now.MONTH)+1));

	YearOptions year = new YearOptions();
	MonthOptions month = new MonthOptions();
	CompanyOptions co = new CompanyOptions();
	
	ProjectCategoryOptions pco = new ProjectCategoryOptions();
	
	GroupOptions go = new GroupOptions();

    String selectedStr1 = "";
    String selectedStr2 = "";
    
    String readOnlyStr = "";
	String buleStr = "";

	formAction = request.getParameter("formAction");
    if (!formAction.equals("Create")) 
	{
        readOnlyStr = "readonly";
		buleStr = "class=read";
    }
    
    ProjectBean project1 = (ProjectBean)request.getAttribute("project");
    if (project1==null) 
	   project1 = new Project();
	 
	String from = project1.getEffective_range_from();
	String year_from = "";
	String month_from = "";
	if (from!=null && from.length()!=0)
	{
		year_from = from.substring(0,4);
		month_from = from.substring(5,7);
	}

	String to = project1.getEffective_range_to();
	String year_to = "";
	String month_to = "";
	if (to!=null && to.length()!=0)
	{
		year_to = to.substring(0,4);
		month_to = to.substring(5,7);
	}
	
	StringBuffer commentsSelectionHtml = new StringBuffer();
	String comments = project1.getComments() == null ? "" : project1.getComments();
	char SPLIT_ON = ',';
	java.util.List tokens = new java.util.ArrayList();
	StringBuffer buffer = new StringBuffer();
	char current, previous = ' ';
	for(int i = 0; i < comments.length(); i++) {
		current = comments.charAt(i);
		if(current == SPLIT_ON) {
			if(buffer.length() == 0) buffer.append(' ');
			tokens.add(buffer.toString());
			buffer = new StringBuffer();
		} else {
			buffer.append(current);
		}
		previous = current;
	}
	if(buffer.length() > 0) tokens.add(buffer.toString());
	String[] data1 = new String[tokens.size()];
	for(int i = 0; i < tokens.size(); data1[i] = (String)tokens.get(i++));
		
		
	String[] data2 = {"Business","Consumer","Corporate","Finance","International","Internal"};
	for(int i=0;i<data2.length;i++) {
		boolean selected = false;
		for(int j=0;j<data1.length;j++) {
			if (data1[j].equals(data2[i])) {
				selected = true;
			}
		}
		if(!selected)
			commentsSelectionHtml.append("<option value=\""+data2[i]+"\">"+data2[i]+"</option>\n");
		else
			commentsSelectionHtml.append("<option value=\""+data2[i]+"\" selected>"+data2[i]+"</option>\n");
	}
%>

<body>
<script language=javascript1.1>
   
    function formValidation()
	{
        var d1 = fm.project_name;
        var d2 = fm.project_category_name;
		var d3 = fm.year_from.value;
		var d4 = fm.month_from.value;
		var d5 = fm.project_group_name;
		var SelBranchVal = "";
		var projectType = d1.value.charAt(0);
		
		for (x=0; x < fm.comments_multi.length; x++) {
			if (fm.comments_multi[x].selected == true) {
				if (SelBranchVal.length == 0)
				    SelBranchVal = fm.comments_multi[x].value ;
				else {
			    	SelBranchVal = SelBranchVal+ "," + fm.comments_multi[x].value ;
				}
			}
		}
		fm.comments.value = SelBranchVal;
    
        if (isNull(d1,"Project Name")||isNull(d2,"Project Category Name") 
        	|| isNull(d5,"Project Group Name"))
		    return false;
        if ((d3.length!=0 && d4.length==0) || (d3.length==0 && d4.length!=0))
	{
			alert("Year and Month should be related!");
			return false;
	}
        if(SelBranchVal.length==0) {
			alert("Business Unit should be selected!");
			return false;
        }
        
        if(!(projectType == "C" || projectType == "S" || projectType == "O" || projectType == "I")) {
			alert("Project Name is in wrong format!");
			return false;
        }
		return true;
    }

</script>

<h2>Project Maintenance</h2>
<form id=fm method=post action='<%=contextPath%>/servlet/tss.project.ProjectServlet' >
<input type=hidden name=formAction value=<%=formAction%> >
<input type=hidden name=comments >
<table cols=10 border=0 >

<tr>
  <td>Project Name:*</td>
  <td>&nbsp;</td>
  <td><input type=text name='project_name' <%=buleStr%> size=20 value='<%=project1.getProject_name()%>' onBlur="this.value=this.value.toUpperCase();" maxlength=20 <%=readOnlyStr%>> </td>
</tr>

<tr>
  <td>Project Category Name:*</td>
  <td>&nbsp;</td>
  <td>
	  <select name='project_category_name' style="width=180;">
		<%=pco.getOptions(project1.getProject_category_name())%>
	  </select>
  </td>
</tr>

<tr>
  <td>Project Group Name:*</td>
  <td>&nbsp;</td>
  <td>
	  <select name='project_group_name' style="width=180;">
		<%=go.getOptions(project1.getProject_group_name(),false)%>
	  </select>
  </td>
</tr>

<tr>
  <td nowrap>Business Unit:*</td>
  <td>&nbsp;</td>
  <td>
    <select name=comments_multi size=6 multiple>
      <%=commentsSelectionHtml.toString()%>
    </select>
  </td>
</tr>

<tr>
  <td>Effective Range:</td>
  <td>&nbsp;</td>
  <td nowrap>From
	 <% if (year_from!=null && year_from.length()!=0) {%>
	  <select name='year_from'>
		<%=year.getOptions(year_from,false)%>
		</select>
	 <% } else { %>
	  <select name='year_from'>
	    <%=year.getOptions(year_now,false)%>
		</select>
		<% } %>	 
  YYYY
  </td>
  <td nowrap>
	 <% if (year_from!=null && year_from.length()!=0) {%>
	    <select name='month_from'>
		<%=month.getOptions(month_from,false)%>
		 </select>
	 <% } else { %>
	  <select name='month_from'>
	   <%=month.getOptions(month_now,false)%>
	    </select>
		<% } %>		
  MM
  </td>
</tr>

<tr>
  <td>&nbsp;</td>
  <td></td>
  <td nowrap>To&nbsp;&nbsp;&nbsp;&nbsp;
	  <select name='year_to'>
		<%=year.getOptions(year_to,false)%>
	  </select>
  YYYY
  </td>
  <td nowrap>
	  <select name='month_to'>
		<%=month.getOptions(month_to,false)%>
	  </select>
  MM
  </td>
</tr>

<tr>
	<td>Chargeable:</td>
	<td>&nbsp;</td>
	<td>
<%
		if (project1.getChargeable_ind().equals("Y"))
			selectedStr1 = "selected";
		if (project1.getChargeable_ind().equals("N"))
			selectedStr2 = "selected";
%>
	  <select name="chargeable_ind">
		 <option value="Y" <%=selectedStr1%> >Yes</option>
		 <option value="N" <%=selectedStr2%> >No</option>
	  </select>
    </td>
</tr>


</table>
<br>
<hr>
<p>
  <INPUT type=button value=Save onclick="save()" >
  <INPUT type=button value=Delete  onclick="deleteRecord()"  >
  <INPUT type=reset  value=Cancel >
  <INPUT type=button value=Back onclick="javascript:history.back()">
</p>
</form>
<%@ include file="../tssfooter.jsp" %>
</body>
</html>